package com.example.spinning.controller;

import com.example.spinning.bean.*;
import com.example.spinning.service.DailyReportService;
import com.example.spinning.service.QualityReportService;
import com.example.spinning.utils.Log;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qualityReport")
public class QualityReportController {
    @Autowired
    QualityReportService qualityReportService;

    //增加记录
    @Log(module = "质量报表",description = "增加质量报表")
    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    public RespBean addNewQualityReport( QualityReport qualityReport) {
        if ("".equals(qualityReport.getName())|| qualityReport.getName() == null) {
            return new RespBean("error", "品名不能为空!");
        }
        if ("".equals(qualityReport.getBatch_no())|| qualityReport.getBatch_no() == null) {
            return new RespBean("error", "批号不能为空!");
        }
        int result = qualityReportService.addNewQualityReport(qualityReport);
        if (result == 1) {
            return new RespBean("success",  "质量信息添加成功");
        } else {
            return new RespBean("error","添加质量信息成功");
        }
    }

    //根据id删除记录
    @Log(module = "质量报表",description = "删除质量报表")
    @RequestMapping(value = "/delete/", method = RequestMethod.DELETE)
    public RespBean deleteById(Long[] ids) {

        boolean result = qualityReportService.deleteQualityReportById(ids);
        if (result) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    //查找
    @RequestMapping(value = "/getByMix/", method = RequestMethod.GET)
    public Map<String, Object> findPage(Integer page, Integer size, String batch_no, String name, String client, Date date){
        PaginationResult pr = qualityReportService.findPage(page, size,batch_no,name,client,date);
        Map<String, Object> map = new HashMap<>();
        map.put("totleCount",pr.getTotal());
        map.put("list",pr.getList());
        return map;
    }

    @Log(module = "质量报表",description = "修改质量报表")
    @RequestMapping(value = "/update/",method = RequestMethod.PUT)
    public RespBean updateById(QualityReport qualityReport){

        int result = qualityReportService.updateCategoryById(qualityReport);
        if (result == 1) {
            return new RespBean("success",  "质量报表修改成功");
        } else {
            return new RespBean("error","质量报表修改失败");
        }
    }



}



